<template>
  <div class="team">
    <div class="team-head">
      <div class="head-lt">
        <h3>My Team</h3>
        <p>Build a team for more revenue</p>
      </div>
      <div class="head-rt">
        <button @click="$router.push('/teamdescription')">
          Team description
        </button>

        <img src="../../assets/imgs/team_dage.png" alt="" srcset="" />
      </div>
    </div>
    <div class="info-card">
      <div class="info-detail">
        <div class="info-lt">
          <p><i class="iconfont icon-jine"></i></p>
          <p class="info-num">{{ team.commission | getmoney }}</p>
          <p class="info-desc">Commission（rs）</p>
        </div>
        <div class="info-rt">
          <p><i class="iconfont icon-team"></i></p>
          <p class="info-num">{{ team.subordinate }}</p>
          <p class="info-desc">My subordinates</p>
        </div>
      </div>
    </div>
    <!-- 列表 -->
    <div class="level-menu">
      <p
        class="menu-item"
        v-for="(m, i) in levelMenu"
        :key="m"
        @click="changeActive(i)"
      >
        <span :class="i == activeMenu ? 'activeMenu' : ''">{{ m }}</span>
      </p>
    </div>
    <div class="leves">
      <van-pull-refresh
        v-model="refreshing"
        loading-text="loading……"
        pulling-text="Drop down to refresh"
        loosing-text="Free to refresh"
        success-text="Refresh the success"
        @refresh="onRefresh"
      >
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="No data！"
          loading-text="loading……"
          :offset="offset"
          @load="onLoad"
        >
          <ul>
            <li v-for="it in listd" :key="it.id">
              <div class="item-lt">
                <p class="item-id">{{ it.name }}</p>
                <p class="item-time">{{ it.created_at }}</p>
              </div>
              <div class="item-rt">+{{ it.amount | getmoney }}</div>
            </li>
          </ul>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      activeMenu: 0,
      levelMenu: ["First level", "Second level", "Third level"],
      team: {},
      current: 1,
      pagenum: 10,
      loading: false,
      finished: false,
      offset: 5,
      listd: [],
      refreshing: false,
    };
  },
  mounted() {
    this.$store.dispatch("teams", { page: 1, limit: 10 }).then((res) => {
      this.team = res;
    });
  },
  methods: {
    changeActive(i) {
      this.activeMenu = i;
      this.listd = [];
      this.current = 1;
      this.finished = false;
      this.loading = true;
      this.$store
        .dispatch("teamusers", {
          page: 1,
          limit: this.pagenum,
          level: this.activeMenu + 1,
        })
        .then((res) => {
          if (res.length > 0) {
            this.listd = this.listd.concat(res);
            this.loading = false;
            this.current += 1;
          } else {
            this.loading = false;
            this.finished = true;
          }
        });
    },
    onLoad() {
      this.$store
        .dispatch("teamusers", {
          page: this.current,
          limit: this.pagenum,
          level: this.activeMenu + 1,
        })
        .then((res) => {
          if (res.length > 0) {
            this.listd = this.listd.concat(res);
            this.loading = false;
            this.current += 1;
          } else {
            this.loading = false;
            this.finished = true;
          }
        });
    },
    onRefresh() {
      this.loading = true;
      this.current = 1;
      this.listd = [];
      this.finished = false;
      this.$store
        .dispatch("teamusers", {
          page: this.current,
          limit: this.pagenum,
          level: this.activeMenu + 1,
        })
        .then((res) => {
          if (res.length > 0) {
            this.listd = this.listd.concat(res);
          }
          this.loading = false;
          this.finished = true;
          this.refreshing = false;
          this.current = 1;
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.team {
  height: 100%;
  .team-head {
    width: 100%;
    height: 144px;
    background: url("../../assets/imgs/team_bg.png");
    background-size: contain;
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
    padding: 38px 24px;
    color: #ffffff;
    h3 {
      font-size: 20px;
    }
    p {
      font-size: 13px;
    }
    button {
      width: 120px;
      height: 30px;
      background: #4a67f5;
      border: 1px solid #ffffff;
      opacity: 1;
      border-radius: 15px;
    }
    .head-rt {
      position: relative;
      img {
        position: absolute;
        right: 0;
        bottom: -43px;
        width: 60px;
        height: 83px;
        z-index: 10;
      }
    }
  }
  .info-card {
    margin: -25px auto 15px auto;
    width: 345px;
    height: 120px;
    background: #ffffff;
    box-shadow: 0px 4px 9px rgba(0, 0, 0, 0.08);
    opacity: 1;
    border-radius: 10px;
    box-sizing: border-box;
    padding: 22px 0;
    .info-detail {
      height: 100%;
      display: flex;
      justify-content: space-around;
      align-items: center;
      text-align: center;
      color: #4a67f5;
      .iconfont {
        font-size: 20px;
      }
      .info-lt {
        flex: 1;
        height: 100%;
        border-right: 1px solid #e2e2e2;
      }
      .info-rt {
        flex: 1;
        height: 100%;
      }
      .info-num {
        font-size: 25px;
        font-weight: bold;
        padding: 5px 0 3px 0;
      }
      .info-desc {
        font-size: 10px;
        color: #888888;
      }
    }
  }

  .level-menu {
    margin: 0 auto;
    width: 346px;
    height: 44px;
    background: #eff1ff;
    border-radius: 10px 10px 0px 0px;
    font-size: 15px;
    color: #333;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .activeMenu {
      color: #4a67f5;
    }
  }
  .leves {
    padding: 0 14px 65px 14px;
    ul {
      background-color: #fff;
      li {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 13px 24px 16px 24px;
        border-bottom: 1px solid #e2e2e2;
        .item-id {
          font-size: 15px;
          color: #333;
        }
        .item-time {
          padding: 7px 0 0 0;
          font-size: 13px;
          color: #888888;
        }
        .item-rt {
          font-size: 24px;
          color: #4a67f5;
        }
      }
    }
  }
}
</style>